package com.suse.lhw.resourceservere.feignTools;

/**
 * @ProjectName: health_manage_system
 * @Package: com.suse.lhw.resourceservere.feignTools
 * @ClassName: ModelExpander
 * @Description: java类作用描述
 * @Author: 李鸿炜
 * @CreateDate: 2020/5/24 20:49
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/5/24 20:49
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */

import com.alibaba.fastjson.JSON;
import feign.Param;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;


@Slf4j
public class ModelExpander implements Param.Expander {

    public String expand(Object value) {
        String objectJson = JSON.toJSONString(value);
        return objectJson;
    }

    public String expandWithName(Object value, String name) {
        String valueExpand = null;

        if (value != null) {
            if (name != null) {
                try {
                    Map<String, Object> jsonMap = (Map<String, Object>) JSON.toJSON(value);

                    Object getValue = jsonMap.get(name);
                    if (getValue != null) {
                        valueExpand = getValue.toString();
                    }
                } catch (Exception e) {
                    log.error("GET VALUE ERROR:", e);
                }
            } else {
                valueExpand = value.toString();
            }
        }

        return valueExpand;
    }
}